package com.njpi.stu.config;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.njpi.stu.attendance.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class CustomUserDetailsService implements UserDetailsService {



    @Autowired
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        QueryWrapper queryWrapper=new QueryWrapper();
        queryWrapper.eq("user_num",username);
        List<String> arr=new ArrayList<>();
        arr.add("ROLE_STUDENT");
        arr.add("ROLE_TEACHER");
        arr.add("ROLE_DBA");
        com.njpi.stu.attendance.entity.User one = userMapper.selectOne(queryWrapper);
        if(one!=null){
            Integer type = one.getType();
            List<GrantedAuthority> authorities=new ArrayList<>();
            authorities.add(new SimpleGrantedAuthority(arr.get(type)));
            User user=new User(username,one.getPassword(),authorities);
            return user;
        }
    return null;


    }
}
